package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.lc.afbx.R;
import com.lc.afbx.adapters.CompanyAdapter;
import com.lc.afbx.conn.GetApiShowICompany;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CompulsoryInsuranceChooseCompanyActivity extends BaseActivity implements View.OnClickListener {


    private Button btnXiayYiBu;
    private ListView mListView;
    private List<GetApiShowICompany.Data> dataList;
    private CompanyAdapter adapter;

    private int selectPoistion = -1;

    private GetApiShowICompany getApiShowICompany = new GetApiShowICompany(new AsyCallBack<GetApiShowICompany.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiShowICompany.Info info) throws Exception {

            dataList = info.dataList;
            mListView.setAdapter(adapter = new CompanyAdapter(context, dataList));
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this);
        setContentView(R.layout.activity_shouye_jiaoqiangxian_xuanzebaoxiangongsi);

        initView();

        setListener();

        getApiShowICompany.tid = "1";
        getApiShowICompany.execute(context);
    }

    private void setListener() {
        btnXiayYiBu.setOnClickListener(this);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (adapter.isCheck.get(position) == false) {
                    adapter.init();
                    selectPoistion = position;
                    adapter.isCheck.put(position, true);
                } else {
                    selectPoistion = -1;
                    adapter.isCheck.put(position, false);
                }
                adapter.notifyDataSetChanged();

            }
        });
    }

    private void initView() {

        findViewById(R.id.back).setOnClickListener(this);
        mListView = ((ListView) findViewById(R.id.conpany_listView));

        btnXiayYiBu = (Button) findViewById(R.id.shouye_btn_xuanzebaoxiangognsi_xiayibu);
    }

    @Override
    public void onClick(View view) {

        Intent intent = null;
        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.shouye_btn_xuanzebaoxiangognsi_xiayibu:
                intent = new Intent(this, CompulsoryInsuranceFillInfoActivity.class);
                if (selectPoistion < 0) {
                    UtilToast.show(context, "请选择保险公司");
                    return;
                } else {

                    Bundle bundle = getIntent().getExtras();

                    UtilLog.e("===========================1", bundle.getString("insurance_type") + "sdfasdfsaasdfas");
                    bundle.putString("cid", dataList.get(selectPoistion).id);
                    bundle.putString("companyName", dataList.get(selectPoistion).title);
                    intent.putExtras(bundle);
                }
                break;

        }
        startActivity(intent);
    }


}
